package gdonly.top.signin.business;

import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import gdonly.top.signin.R;
import gdonly.top.signin.entity.User;
import gdonly.top.signin.utils.DatabaseQuery;
import gdonly.top.signin.utils.WifiUtil;

public class SignByStuActivity extends AppCompatActivity {
    private Button signInBtn;
    private ImageView showSuccessImg;
    private TextView showSuccessText;
    private Button queryBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_by_stu);
        initViews();
    }
    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.sign_in_btn:

                    LocationManager alm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                    if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
                        signIn();
                    }
                    else {

                        Toast.makeText(SignByStuActivity.this, "请开启GPS！", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        startActivityForResult(intent, 0); //此为设置完成后返回到获取界面
                    }

                    break;
                case R.id.queryself_in_btn:
                    String userId = ((Application)getApplication()).getThisUser().getId();
                    Intent intent = new Intent(SignByStuActivity.this, QueryRecordsActivity.class);
                    intent.putExtra("user_id",userId);
                    startActivity(intent);
                    break;
            }
        }
    };
    private void initViews(){
        signInBtn = (Button) findViewById(R.id.sign_in_btn);
        signInBtn.setOnClickListener(listener);
        queryBtn = (Button) findViewById(R.id.queryself_in_btn);
        queryBtn.setOnClickListener(listener);

        showSuccessImg = (ImageView) findViewById(R.id.show_success_img);
        showSuccessText = (TextView) findViewById(R.id.show_success_text);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0){
           // signIn();
        }
    }

    private void signIn()  {
        User user = ((Application)getApplication()).getThisUser();
        //String id = user.getId();
        if(DatabaseQuery.queryEnableSign(SignByStuActivity.this,  user)) {//签到开关已经打开

            User monitor = DatabaseQuery.getMonitorInfo(SignByStuActivity.this, user);//得到班长最新的信息

            if(monitor != null) {
                if (WifiUtil.includeAp(SignByStuActivity.this, monitor.getSsid(), monitor.getMac())) {
                    // 周围包含班长的ssid以及mac热点
                    DatabaseQuery.sign(SignByStuActivity.this, user);
                    signInBtn.setVisibility(View.INVISIBLE);
                    showSuccessText.setVisibility(View.VISIBLE);
                    showSuccessImg.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(SignByStuActivity.this, "请你离班长再近一点～", Toast.LENGTH_SHORT).show();
                }
            }
        }
        else{
            Toast.makeText(SignByStuActivity.this, "签到尚未开始～", Toast.LENGTH_SHORT).show();
        }
    }
}
